// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package quicksight

import (
	"context"

	"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
	"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
	"github.com/hashicorp/terraform-provider-aws/internal/conns"
	"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
	quicksightschema "github.com/hashicorp/terraform-provider-aws/internal/service/quicksight/schema"
	"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_quicksight_group", name="Group")
func dataSourceGroup() *schema.Resource {
	return &schema.Resource{
		ReadWithoutTimeout: dataSourceGroupRead,

		SchemaFunc: func() map[string]*schema.Schema {
			return map[string]*schema.Schema{
				names.AttrARN: {
					Type:     schema.TypeString,
					Computed: true,
				},
				names.AttrAWSAccountID: quicksightschema.AWSAccountIDDataSourceSchema(),
				names.AttrDescription: {
					Type:     schema.TypeString,
					Computed: true,
				},
				names.AttrGroupName: {
					Type:     schema.TypeString,
					Required: true,
				},
				names.AttrNamespace: quicksightschema.NamespaceDataSourceSchema(),
				"principal_id": {
					Type:     schema.TypeString,
					Computed: true,
				},
			}
		},
	}
}

func dataSourceGroupRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
	var diags diag.Diagnostics
	conn := meta.(*conns.AWSClient).QuickSightClient(ctx)

	awsAccountID := meta.(*conns.AWSClient).AccountID(ctx)
	if v, ok := d.GetOk(names.AttrAWSAccountID); ok {
		awsAccountID = v.(string)
	}
	groupName := d.Get(names.AttrGroupName).(string)
	namespace := d.Get(names.AttrNamespace).(string)
	id := groupCreateResourceID(awsAccountID, namespace, groupName)

	group, err := findGroupByThreePartKey(ctx, conn, awsAccountID, namespace, groupName)

	if err != nil {
		return sdkdiag.AppendErrorf(diags, "reading QuickSight Group (%s): %s", id, err)
	}

	d.SetId(id)
	d.Set(names.AttrARN, group.Arn)
	d.Set(names.AttrAWSAccountID, awsAccountID)
	d.Set(names.AttrDescription, group.Description)
	d.Set(names.AttrGroupName, group.GroupName)
	d.Set("principal_id", group.PrincipalId)

	return diags
}
